<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('新增站内邮箱')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-xiaoxi-add">
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">邮件标题</label>
                <div class="col-sm-8">
                    <textarea style="height: 120px" name="biaoti" class="form-control" required></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">邮件内容</label>
                <div class="col-sm-8">
                    <textarea style="height: 400px" name="neirong" class="form-control" required></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">接收人</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <input name="jieshouren" onclick="selectJieshourenTree()" id="jieshourennames" type="text"
                               class="form-control" th:value="${jieshouren}" required>
                        <input name="jieshourenid" id="jieshourenids" class="form-control" style="display:none"  th:value="${jieshourenid}">
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-12" style="text-align: center;margin-top: 20px">
                    <button type="button" class="btn btn-primary btn-lg" onclick="submitHandler(1)" shiro:hasPermission="system:xiaoxi:add"><i class="fa fa-check">发送邮件</i></button>
                    <button type="button" class="btn btn-success btn-lg" onclick="submitHandler(0)" shiro:hasPermission="system:xiaoxi:add"><i class="fa fa-paste">保存草稿</i></button>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var prefix = ctx + "system/xiaoxi"
        $("#form-xiaoxi-add").validate({
            focusCleanup: true
        });

        function doSubmit(index, layero) {
            var iframeWin = $(layero).find("iframe")[0].contentWindow //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method()
            var rows = iframeWin.getSelections();
            var jieshourenids=new Array();
            var jieshourennames=new Array();
            if(rows.length>0){
                for (var i = 0; i < rows.length; i++) {
                    jieshourenids.push(rows[i]["userId"]);
                    jieshourennames.push(rows[i]["userName"]);
                }
                $("#jieshourenids").val(jieshourenids);
                $("#jieshourennames").val(jieshourennames);
            }
            $.modal.close(index);
        }
        /* 选择接收人 */
        function selectJieshourenTree() {
            $.modal.open("选择接收人", "/system/xiaoxi/JieshourenTree", 1400 ,800,doSubmit);
        }

        function submitHandler(s) {
            if ($.validate.form()) {
                var formdata = $('#form-xiaoxi-add').serialize()
                formdata += "&zhuangtai=" + s;
                $.operate.save(prefix + "/add", formdata,(res)=>{
                    if (res.code == "0") {
                        if(s==1) {
                            layer.open({
                                time: 1000,
                                btn: [],
                                content: "发送邮件成功!",
                            });
                        }else if(s==0){
                            layer.open({
                                time: 1000,
                                btn: [],
                                content: "保存草稿成功!",
                            });
                        }
                        $.form.reset();
                    }
                });
            }
        }
    </script>
</body>
</html>